<template>
  <!-- 表单 -->
  <el-form label-width="120px" ref="formData" :model="formData" :rules="rules">
    <el-form-item label="姓名" prop="username">
      <el-input
        style="width:50%"
        v-model="formData.username"
        placeholder="请输入姓名"
      />
    </el-form-item>
    <el-form-item label="手机" prop="mobile">
      <el-input
        style="width:50%"
        v-model="formData.mobile"
        placeholder="请输入手机号"
      />
    </el-form-item>
    <el-form-item label="入职时间" prop="timeOfEntry">
      <el-date-picker
        style="width:50%"
        v-model="formData.timeOfEntry"
        placeholder="请选择入职时间"
      />
    </el-form-item>
    <el-form-item label="聘用形式" prop="formOfEmployment">
      <el-select
        style="width:50%"
        v-model="formData.formOfEmployment"
        placeholder="请选择"
      >
        <el-option
          v-for="(item, index) in hireType"
          :key="index"
          :label="item.value"
          :value="item.id"
        >
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="工号" prop="workNumber">
      <el-input
        style="width:50%"
        v-model="formData.workNumber"
        placeholder="请输入工号"
      />
    </el-form-item>
    <el-form-item label="部门" prop="departmentName">
      <!-- readonly只读 -->
      <el-input
        style="width:50%"
        v-model="formData.departmentName"
        placeholder="请选择部门"
        @focus="departmentNameFocus"
      />
      <div class="tree-box">
        <el-tree
          v-show="showTree"
          :data="treeData"
          default-expand-all
          :props="{ label: 'name' }"
          @node-click="treeClick"
        />
      </div>
    </el-form-item>
    <el-form-item label="转正时间" prop="correctionTime">
      <el-date-picker
        style="width:50%"
        v-model="formData.correctionTime"
        placeholder="请选择转正时间"
      />
    </el-form-item>
    <el-form-item>
      <el-button @click="cancel">取消</el-button>
      <el-button @click="confirmFn" type="primary">确定</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import Employees from '@/api/constant'
export default {
  props: {
    treeData: {
      type: Array,
      default: () => []
    }
  },
  name: 'EmpDialog',
  data () {
    return {
      // 添加表单字段
      formData: {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      // 添加表单的校验字段
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: '手机号格式不正确',
            trigger: 'blur'
          }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: 'blur' }
        ]
      },
      //聘用形式枚举数组+对象
      hireType: Employees.hireType,
      //部门树形控件(显示、隐藏)
      showTree: false,
      //   点击的部门名字(选择)
      clickDepartName: ''
    }
  },
  watch: {
    //   检测- 用户在也买你修改后赋予的新值(newVal)
    // 检测- 点击树形控件时,给此对象树形赋值也会触发此函数
    // 检测 - resetFields置空的时候也触发了
    'formData.departmentName' (newVal) {
      // 证明要置空
      if (newVal.length === 0) {
        // 阻止代码往下执行
        return
      }
      // 证明用户在输入框修改
      if (newVal !== this.clickDepartName) {
        //立即把用户改的, 覆盖掉
        this.formData.departmentName = this.clickDepartName
      }
    }
  },
  methods: {
    //   dialog框取消按钮
    cancel () {
      this.$emit('update:showDialog', false)
    },
    //   dialog框确定按钮
    confirmFn () {
      this.$refs.formData.validate(valid => {
        if (valid) {
          this.$emit('addEmpEV', { ...this.formData })
          this.$emit('update:showDialog', false)
        }
      })
    },
    // 部门输入框聚焦
    departmentNameFocus () {
      this.showTree = true
    },
    // 部门树形控件 -> 行点击事件
    // data : 点击那一行对应的数据对象
    // 折叠效果自带的, 写了return, 不像让下面隐藏tree组件的那句话执行
    // 效果 : 你点击只看到折叠, 没有填充和隐藏
    treeClick (data) {
      if (data && data.children) {
        //   你点击在了折叠的行上(回去吧)
        return
      }
      this.formData.departmentName = data.name
      //   把点击的名字赋值一遍
      this.clickDepartName = data.name
      this.showTree = false
    }
  }
}
</script>

<style lang="scss" scoped></style>
